package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SalChance;
import com.czkt.pojo.SalPlan;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SalPlanVo;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.ISalChanceService;
import com.czkt.service.ISalPlanService;
import com.czkt.util.PageUtile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liu
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/plan")
public class SalPlanController {

    @Autowired
    ISalPlanService salPlanService;

    @Autowired
    ISalChanceService salChanceService;
    /**
     * 获取客户开发计划
     *
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public Object getPlanList(SalChance salChance,
                              @RequestParam(defaultValue = "1") Integer pageIndex,
                              @RequestParam(defaultValue = "4") Integer pageSize,
                              Model model) {

        IPage<SalPlanVo> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageIndex);

        SalPlanVo salPlanVo = new SalPlanVo();
        salPlanVo.setSalChance(salChance);

        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        salChance.setChcDueId(currentUser.getUsrId());

        IPage<SalPlanVo> page1 = salPlanService.getPlanVoPage(page, salPlanVo);

        PageUtile<SalPlanVo> pageUtile = new PageUtile();

        BeanUtils.copyProperties(page1,pageUtile);

        model.addAttribute("planPage",pageUtile);

        return "plan/list";

    }





    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/add")
    public Object toAdd(Model model) {
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<SalChance>();
        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        queryWrapper.lambda()
                .eq(currentUser.getRole().getRoleId() == 2, SalChance::getChcDueId, currentUser.getUsrId())
        ;
        model.addAttribute("users", salChanceService.list(queryWrapper));
        return "plan/add";
    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/save")
    @Transactional
    public Object saveOrUpdate(SalPlan salPlan, Model model) {

        boolean result = salPlanService.saveOrUpdate(salPlan);
        if (result) {


            return "redirect:/plan/list";
        } else if (salPlan.getPlaId() == null) {
            return "redirect:/plan/add";
        } else {
            return "redirect:/plan/edit?usrId=" + salPlan.getPlaId();
        }

    }


    /**
     * 定义跳转到修改页的方法
     */
    @RequestMapping("/edit")
    public Object toEdit(long plaId, Model model) {
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<SalChance>();
        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        queryWrapper.lambda()
                .eq(currentUser.getRole().getRoleId() == 2, SalChance::getChcDueId, currentUser.getUsrId())
        ;
        model.addAttribute("users", salChanceService.list(queryWrapper));
        model.addAttribute("salPlan", salPlanService.getPlanVoById(plaId));
        return "plan/edit";
    }


    /**
     * 定义跳转到执行页的方法
     */
    @RequestMapping("/excute")
    public Object toexcute(long plaId, Model model) {
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<SalChance>();
        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        queryWrapper.lambda()
                .eq(currentUser.getRole().getRoleId() == 2, SalChance::getChcDueId, currentUser.getUsrId())
        ;
        model.addAttribute("users", salChanceService.list(queryWrapper));
        model.addAttribute("salPlan", salPlanService.getPlanVoById(plaId));
        return "plan/excute";
    }


    /**
     * 定义跳转到修改页的方法
     */
    @RequestMapping("/del/{plaId}")
    @ResponseBody
    public Object del(@PathVariable long plaId, Model model) {
        QueryWrapper<SalPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SalPlan::getPlaId ,plaId)
                .isNull(SalPlan::getPlaResult);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("delResult",salPlanService.remove(queryWrapper));
        return resultMap;

    }

}
